﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.IO;
using System.Drawing;
using CDB.LinqModel.Data;
using System.Web;
using System.Net;

namespace CDB.LinqModel
{
    public class CDBGeneral
    {
        #region Method
        public static string FirstLetterCapital(string inputString)
        {
            if (inputString != null && inputString != string.Empty)
            {
                inputString = inputString.ToString().Substring(0, 1).ToUpper() + inputString.ToString().Substring(1, inputString.ToString().Length - 1).ToLower();
            }
            return inputString;
        }
        public static string SingleToDouble(string strValue)
        {
            strValue = strValue.Replace("'", "''").Trim();
            return strValue;
        }
        public static string DoubleToSingle(string strValue)
        {
            if (strValue != null)
            {
                strValue = strValue.Replace("''", "'").Trim();
            }
            return strValue;
        }
        #endregion

        #region Send Email
        public static void sendEmailToUsers(string emailTo, string Subject, string mailBody, string EmailCC)
        {
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com");
            client.EnableSsl = true;//for hexa its true, for live on godday its false
            client.Credentials = new System.Net.NetworkCredential("support@hexatechnolabs.com", "support1353");
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            mail.IsBodyHtml = true;
            mail.Subject = Subject.ToString();
            mail.From = new MailAddress("support@hexatechnolabs.com");
            mail.To.Add(emailTo);
            if (EmailCC != "")
                mail.CC.Add(EmailCC);
            mail.Body = mailBody.ToString();
            try
            {
                client.Send(mail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Thumb Image
        public static void createThumbImages(string src, string dest, int thumbWidth)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(src);
            int srcWidth = image.Width;
            int srcHeight = image.Height;
            int thumbHeight = (srcHeight / srcWidth) * thumbWidth;
            Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);

            System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
            gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

            bmp.Save(dest);

            bmp.Dispose();
            image.Dispose();
        }

        public static byte[] CreateThumbnail(string path, int maxHeight, int maxWidth, string extension, string dest, string filename)
        {

            System.Drawing.Image img = System.Drawing.Image.FromFile(path);

            extension = extension.ToLower();



            byte[] buffer = null;

            try
            {

                int width = img.Size.Width;

                int height = img.Size.Height;



                bool doWidthResize = (maxWidth > 0 && width > maxWidth && width > maxHeight);

                bool doHeightResize = (maxHeight > 0 && height > maxHeight && height > maxWidth);



                //only resize if the image is bigger than the max

                if (doWidthResize || doHeightResize)
                {

                    int iStart;

                    Decimal divider;

                    if (doWidthResize)
                    {

                        iStart = width;

                        divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);

                        width = maxWidth;

                        height = (int)Math.Round((Decimal)(height / divider));

                    }

                    else
                    {

                        iStart = height;

                        divider = Math.Abs((Decimal)iStart / (Decimal)maxHeight);

                        height = maxHeight;

                        width = (int)Math.Round((Decimal)(width / divider));

                    }

                    System.Drawing.Image newImg = img.GetThumbnailImage(width, height, null, new System.IntPtr());

                    try
                    {
                        string fname = dest + filename;
                        newImg.Save(fname);
                    }
                    finally
                    {
                        newImg.Dispose();
                    }
                }
                else
                {
                    //Copy the same image into the thumb folder.
                    File.Copy(path, dest + "//" + filename);
                }
            }
            catch
            {
                System.Drawing.Image imNoimage = System.Drawing.Image.FromFile("/images/noimage.gif");
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        imNoimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                        buffer = ms.ToArray();
                    }
                }
                finally
                {
                    imNoimage.Dispose();
                }
            }
            finally
            {
                img.Dispose();
            }
            return buffer;
        }
        #endregion

        #region General Methods
        public static string getIPAddress(HttpContext currentContext)
        {
            object obj = new object();
            obj = currentContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (obj == null)
            {
                obj = currentContext.Request.ServerVariables["REMOTE_ADDR"];
            }
            return obj.ToString();
        }

        public static string JustSendSMS(string smsUrl, string strUser, string strPassword, string strRecip, string strMsgText, string strSMSScheduleDate)
        {
            string strUrl = string.Empty;
            strUrl = smsUrl;
            strUrl += "?ID=" + HttpUtility.UrlEncode(strUser) +
                      "&Pwd=" + HttpUtility.UrlEncode(strPassword) +
                      "&PhNo=" + HttpUtility.UrlEncode(strRecip) +
                      "&Text=" + HttpUtility.UrlEncode(strMsgText);

            if (strSMSScheduleDate != "")
                strUrl = strUrl + "&ScheduleAt=" + HttpUtility.UrlEncode(strSMSScheduleDate);

            Uri objURI = new Uri(strUrl);
            WebRequest objWebRequest = WebRequest.Create(objURI);
            WebResponse objWebResponse = objWebRequest.GetResponse();
            Stream objStream = objWebResponse.GetResponseStream();
            StreamReader objStreamReader = new StreamReader(objStream);
            return objStreamReader.ReadToEnd();
        }
        #endregion

        #region IPAddress
        public static string ipaddress(System.Web.HttpContext currentContext)
        {
            object obj = new object();
            obj = currentContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (obj == null)
            {
                obj = currentContext.Request.ServerVariables["REMOTE_ADDR"];
            }
            return obj.ToString();
        }

        #endregion

        #region Enums
        public enum Month
        {
            Jan = 1,
            Feb = 2,
            Mar = 3,
            Apr = 4,
            May = 5,
            Jun = 6,
            Jul = 7,
            Aug = 8,
            Sep = 9,
            Oct = 10,
            Nov = 11,
            Dec = 12
        }
        #endregion
    }
}
